home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / INFO / TSFAQP18.ZIP / FAQPAS.TXT next >
Internet Message Format  |  1993-12-26  |  69KB

  1. From ts@uwasa.fi Sun Dec 26 00:00:00 1993
  2. Subject: FAQPAS.TXT contents
  3.                                   Copyright (c) 1993 by Timo Salmi
  4.                                                All rights reserved
  5.  
  6. FAQPAS.TXT Frequently (and not so frequently) asked Turbo Pascal
  7. questions with Timo's answers. The items are in no particular order.
  8.  
  9. You are free to quote brief passages from this file provided you
  10. clearly indicate the source with a proper acknowledgment.
  11.  
  12. Comments and corrections are solicited. But if you wish to have
  13. individual Turbo Pascal consultation, please rather post your
  14. questions to a suitable UseNet newsgroup like comp.lang.pascal. It
  15. is much more efficient than asking me by email. I'd like to help,
  16. but I am very pressed for time. I prefer to pick the questions I
  17. answer from the Usenet news. Thus I can answer publicly at one go if
  18. I happen to have an answer. Besides, newsgroups have a number of
  19. readers who might know a better or an alternative answer. Don't be
  20. discouraged, though, if you get a reply like this from me. I am
  21. always glad to hear from fellow Turbo Pascal users.
  22.  
  23. If you are an experienced Turbo Pascal programmer with Turbo Pascal
  24. material you would like to circulate publicly world-wide, you are
  25. welcome to submit your material to the Garbo MsDos archives at the
  26. University of Vaasa. To do that you *FIRST* must *CAREFULLY* read
  27. the uploading instructions and the formal requirements in the file
  28. garbo.uwasa.fi:/pc/UPLOAD.INF. You are also welcome to contact me by
  29. email for these instructions if you are not familiar with
  30. downloading them from the Garbo archives.
  31.  
  32. ..................................................................
  33. Prof. Timo Salmi      Co-moderator of comp.archives.msdos.announce
  34. Moderating at garbo.uwasa.fi anonymous FTP  archives  128.214.87.1
  35. Faculty of Accounting & Industrial Management; University of Vaasa
  36. Internet: ts@uwasa.fi   BBS +(358)-61-3170972; FIN-65101,  Finland
  37.  
  38. -------------------------------------------------------------------
  39.  1) How do I disable or capture the break key in Turbo Pascal?
  40.  2) How do I get a printed documentation of my students' TP runs?
  41.  3) What is the code for the weekday of a given date?
  42.  4) Need a program to format Turbo Pascal source code consistently
  43.  5) Can someone give me advice for writing a tsr program?
  44.  6) Why can't I read / write the com ports?
  45.  7) What are interrupts and how to use them in Turbo Pascal?
  46.  8) Should I upgrade my Turbo Pascal version?
  47.  9) How do I execute an MsDos command from within a TP program?
  48. 10) How is millisecond timing done?
  49. 11) How can I customize the text characters to my own liking?
  50. 12) How to find the files in a directory and subdirectories?
  51. 13) I need a power function but there is none in Turbo Pascal.
  52. 14) How can I create arrays that are larger than 64 kilobytes?
  53. 15) How can I test that the printer is ready?
  54. 16) How can I clear the keyboard type-ahead buffer?
  55. 17) How can I utilize expanded memory (EMS) in my programs?
  56. 18) How can I obtain the entire command line?
  57. 19) How do I redirect text from printer to file in my TP program?
  58. 20) Turbo Pascal is for wimps. Use standard Pascal or C instead?
  59. 21) How do I turn the cursor off?
  60. 22) How to find all roots of a polynomial?
  61. 23) What is all this talk about "Pascal homework on the net"?
  62. 24) How can I link graphics drivers directly into my executable?
  63. 25) How can I trap a runtime error?
  64. 26) How to get ansi control codes working in Turbo Pascal writes?
  65. 27) How to evaluate a function given as a string to the program?
  66. 28) How does one detect whether input (or output) is redirected?
  67. 29) How does one set the 43/50 line text mode?
  68. 30) How can I assign a value to an environment variable in TP?
  69. -------------------------------------------------------------------
  70.  
  71. Unless otherwise stated the answers cover versions 4.0, 5.0, 5.5,
  72. 6.0 and 7.0 (real mode). The Q&As are not for Turbo Pascal version 3
  73. or earlier. Objects, TVision, or Windows are not covered. (I do not
  74. use them myself.)
  75.  
  76. From ts@uwasa.fi Sun Dec 26 00:00:01 1993
  77. Subject: Disabling or capturing the break key
  78.  
  79. 1. *****
  80.  Q: I don't want the Break key to be able to interrupt my TP
  81. programs. How is this done?
  82.  Q2: I want to be able to capture the Break key in my TP program.
  83. How is this done?
  84.  Q3: How do I detect if a certain key has been pressed?
  85.  
  86.  A: This very frequently asked question is basically a case of RTFM
  87. (read the f*ing manual). But this feature is, admittedly, not very
  88. prominently displayed in the Turbo Pascal reference. (As a general
  89. rule we should not use the newsgroups as a replacement for our
  90. possibly missing manuals, but enough of this line.)
  91.    There is a CheckBreak variable in the Crt unit, which is true by
  92. default. To turn if off use
  93.      uses Crt;
  94.      :
  95.      CheckBreak := false;
  96.      :
  97. Besides turning off break checking this enables you to capture the
  98. pressing of the break key as you would capture pressing ctrl-c. In
  99. other words you can use e.g.
  100.      :
  101. procedure TEST;
  102. var key : char;
  103. begin
  104.   repeat
  105.     if KeyPressed then
  106.       begin
  107.         key := ReadKey;
  108.         case key of
  109.           #3 : begin writeln ('Break'); exit; end;  {ctrl-c or break}
  110.           else write (ord(key), ' ');
  111.         end; {case}
  112.       end; {if}
  113.   until false;
  114. end;  (* test *)
  115.      :
  116. IMPORTANT: Don't test the ctrl-break feature just from within the TP
  117. IDE, because it has ctlr-break handler ("intercepter") of its own
  118. and may confuse you into thinking that ctrl-break cannot be
  119. circumvented by the method given above.
  120.   The above example has a double purpose. It also shows the
  121. rudiments how you can detect if a certain key has been pressed. This
  122. enables you to give input without echoing it to the screen, which is
  123. a later FAQ in this collection.
  124.   This is, however, not all there can be to break checking, since
  125. the capturing is possible only at input time. It is also possible to
  126. write a break handler to interrupt a TP program at any time. For
  127. more details see Ohlsen & Stoker, Turbo Pascal Advanced Techniques,
  128. Chapter 7. (For the bibliography, see FAQPASB.TXT in this same FAQ
  129. collection).
  130. --------------------------------------------------------------------
  131.  
  132. From ts@uwasa.fi Sun Dec 26 00:00:02 1993
  133. Subject: Directing output also to printer
  134.  
  135. 2. *****
  136.  Q: I want to have a printed documentation of my students' Turbo
  137. Pascal program exercises. How is all input and output directed also
  138. to the printer?
  139.  
  140.  A1: Use a screen capturing program to put everything that comes
  141. onto the screen into a file, and print the file. See FAQPROGS.TXT in
  142. /pc/ts/tsfaqn39.zip (or whatever version number is the current) for
  143. more about these programs. Available by anonymous FTP or mail server
  144. from garbo.uwasa.fi.
  145.  
  146.  A2: See the code in TSPAS.NWS (item: Redirecting writes to the
  147. printer) in the /pc/ts/tspa33??.zip (or whatever is the current
  148. version number) Turbo Pascal units package (?? = 40, 50, 55, 60, or
  149. 70 depending on your TP version). Alternatively use USECON and
  150. USEPRN routines in the TSUNTG unit of the same package.
  151.  
  152.      +------------------------------------------+
  153.      ! To get these and other packages given as !
  154.      !   /dir/subdir/name                       !
  155.      ! see the instructions in PD2ANS.TXT       !
  156.      +------------------------------------------+
  157.  
  158.  A3: But the really elegant solution to the problem of getting a
  159. logfile (or a printed list) of a Turbo Pascal run is to rewrite the
  160. write(ln) and read(ln) device driver functions. In itself writing
  161. such driver redirections is very advanced Turbo Pascal programming,
  162. but when the programming has once been done, the system is extremely
  163. easy to use as many times as you like. It goes like this. The driver
  164. redirections are programmed into a unit (say, tpulog or tpuprn). All
  165. that is needed after that is to include the following uses statement
  166. into the program (the target program) which has to be logged:
  167.       uses TPULOG;    ( or )    uses TPUPRN;
  168. This is all there is to it. Just adding one simple line to the
  169. target program. (If you call any other units, "uses tpulog" must
  170. come AFTER the system units (eg Dos), but BEFORE any which you may
  171. define yourself!)
  172.    The reason that I have named two units here instead of just one
  173. in the above example is that the preferred log for the target
  174. program may be a logfile or the printer. The better solution of
  175. these two is to use the logfile option, and then print it. The
  176. reason is simple. If the target program itself prints something,
  177. your printout will look confused.
  178.    The logging also has obvious limitations. It works for standard
  179. input and output (read(ln) and write(ln)) only. 1) It does not
  180. support graphics, in other words it is for the textmode. 2) It does
  181. not support direct (Crt) screen writes. 3) And, naturally it only
  182. shows the input and output that comes to the screen. Not any other
  183. input or output, such as from or to a file. 4) Furthermore, you are
  184. not allowed to reassign input or output. Statements like assign
  185. (output, '') will result in a crash, because the rewritten output
  186. device redirections are invalidated by such statements. 5) The
  187. device on the default drive must not be write protected, since else
  188. the logfile cannot be written to it. 6) It does not work for Turbo
  189. Pascal 4.0. Despite these restrictions, the method is perfectly
  190. suited for logging students' Turbo Pascal escapades.
  191.    It is advisable first to test and run your target program without
  192. "tpulog", so that if you get any strange errors you'll know whether
  193. they are caused by the logging.
  194.    Where to get such a unit. The code can be found in Michael
  195. Tischer (1990), Turbo Pascal Internals, Abacus, Section 4.2. Next a
  196. few of my own tips on this unit Tischer calls Prot. 1) The code is
  197. in incorrect order. The code that is listed on pages 142 - 145 goes
  198. between pages 139 and 140. 2) You can change the logfile name (const
  199. prot_name) to lpt1 for a printed list of the target program run. In
  200. that case it is advisable to include a test for the online status of
  201. the printer within Tischer's unit. 3) I see no reason why the two
  202. lines in Tischer's interface section couldn't be transferred to the
  203. implementation section. Why have any global definitions?  But all in
  204. all, it works like magic!
  205.  
  206.  A4: From: abcscnuk@csunb.csun.edu (Naoto Kimura (ACM))
  207. Subject: Re: Printing a log of students' exercises revisited
  208. To: ts@uwasa.fi
  209. Date: Fri, 2 Nov 90 20:52:03 pdt
  210. [Reproduced with Naoto's kind permission]
  211. By the way, several months ago, I had submitted a file (nktools.zip)
  212. file on SimTel that contains sources to a unit (LOGGER), which
  213. allows logging of I/O going through the standard input and output
  214. files, while still being able to use the program interactively.  I
  215. believe that I also submitted a copy to your site.  It was something
  216. I put together for use by students here at California State
  217. University at Northridge.  The source works equally well in all
  218. presently available versions of Turbo Pascal.
  219. The only requirements are that
  220.  * you place it as one of the last entries in the USES clause.  If
  221.    there is anything that redirects the standard input and output
  222.    file variables, you should put that unit before my unit in the
  223.    USES clause, so that it can see the I/O stream.
  224.  * Don't use the GotoXY and similar screen display control
  225.    procedures in the Crt unit and expect it to come out the same way
  226.    you had it on the display.  Since all my unit does is just
  227.    capture the I/O stream to send it through the normal channels and
  228.    also to the log file, all screen control information is not sent
  229.    to the log file.
  230.  * All I/O you want logged should go through the standard input and
  231.    output file variables.
  232.  * Don't close the standard input and output file variables, because
  233.    it will cause problems.  Basically, as far as I have checked, it
  234.    just causes the logging to stop at that point.
  235. --------------------------------------------------------------------
  236.  
  237. From ts@uwasa.fi Sun Dec 26 00:00:03 1993
  238. Subject: Code to give the weekday of a date
  239.  
  240. 3. *****
  241.  Q: I want code that gives the weekday of the given date.
  242.  
  243.  A1: There is a WKDAYFN function in /pc/ts/tspa33*.zip (or whatever
  244. version number is the latest, and where * is 40 50 55 60 and 70)
  245. Turbo Pascal units collection to give the modern weekday based on
  246. Zeller's congruence. Available by anonymous FTP or mail server from
  247. garbo.uwasa.fi. Also you can find a more extensive Julian and
  248. Gregorian weekday algorithm with source code in Dr.Dobb's Journal,
  249. June 1989, p. 148. Furthermore Press & Flannery & al (1986),
  250. Numerical Recipes, Cambridge University Press, present a weekday
  251. code. The Numerical Recipes codes are available as
  252. /pc/turbopas/nrpas13.zip (big, 404k!).
  253.  
  254.  A2: Some will recommend the following kludge. Store the current
  255. date, change it, and let MsDos get you the weekday. Don't use it! It
  256. is a bad suggestion. On top of being sloppy programming, there are
  257. several snags.  The trick works only for years 1980-2079. A crash
  258. the program may leave the clock at a wrong date. And even if
  259. multitasking is rare, in a multitasking environment havoc may result
  260. for the other tasks. And you may have a TSR that requires the
  261. correct date, etc.
  262. --------------------------------------------------------------------
  263.  
  264. From ts@uwasa.fi Sun Dec 26 00:00:04 1993
  265. Subject: Pretty printers (or uniform code)
  266.  
  267. 4. *****
  268.  Q: Where can I find a program that formats my (or my students')
  269. Turbo Pascal code in a consistent matter.
  270.  
  271.  A: What you are asking for is often called "a pretty printer".
  272. TurboPower Software's (the usual disclaimer applies) commercial
  273. Turbo Analyst has a facility for this with many options. There are
  274. also PD and shareware pretty printers, such as /pc/turbopas/
  275. pritprn.zip, bp7sb*.zip "Source Beautifier for Borland Pascal by
  276. J.Ferincz", and others at garbo.uwasa.fi available by anonymous FTP
  277. or mail server. See /pc/INDEX.ZIP for a list of the files.
  278. --------------------------------------------------------------------
  279.  
  280. From ts@uwasa.fi Sun Dec 26 00:00:05 1993
  281. Subject: How to write TSR programs
  282.  
  283. 5. *****
  284.  Q: Can someone give me advice for writing a tsr program.
  285.  
  286.  A: Writing a terminate and stay resident program can be considered
  287. advanced programming and is beyond the scope of an electronic
  288. message with limited space. Instead, here are some references to
  289. Turbo Pascal books and papers which have a coverage of the subject.
  290. Stephen O'Brien, Turbo Pascal, The Complete Reference, Chapter 16;
  291. Stephen O'Brien, Turbo Pascal, Advanced Programmer's Guide, Chapter
  292. 6; Michael Tischer, Turbo Pascal Internals, Chapter 11 (a definite
  293. bible of TP programming!); Michael Ticher (1992), PC Intern System
  294. Programming, Chapter 32; Michael Yester, Using Turbo Pascal, Chapter
  295. 19; Kent Pottebaum, "Creating TSR Programs with Turbo Pascal", Dr.
  296. Dobb's Journal, May 1989 and June 1989; Kris Jamsa, Dos Power User's
  297. Guide, pp. 649-; Edward Mitchell (1993), Borland Pascal Developer's
  298. Guide, Section "Writing TSRs", pp. 370-400, with 778 lines of sample
  299. code.
  300. --------------------------------------------------------------------
  301.  
  302. From ts@uwasa.fi Sun Dec 26 00:00:06 1993
  303. Subject: Programming com ports
  304.  
  305. 6. *****
  306.  Q: Why can't I read / write the com ports.
  307.  
  308.  A: Com port programming (most often writing telecommunication
  309. programs) is much much more complicated than simply trying to use
  310.   write (com, whatever);
  311.   read  (com, whatever);
  312. This is a very advanced subject (frankly, beyond me), and the best
  313. way to learn is to try to obtain some code to show you how. One
  314. place to look at is Turbo Pascal text-books (I have a long list of
  315. them at garbo.uwasa.fi archives in /pc/ts/tsfaqp*.zip. There also is
  316. an example by David Rind in garbo.uwasa.fi/pc/pd2/faquote.zip.
  317. Another source is International FidoNet pascal conference at some
  318. bulletin board near you. The conference has had some very good
  319. discussions in it.  (No, I don't have them stored for distribution,
  320. nor any further information.)
  321. --------------------------------------------------------------------
  322.  
  323. From ts@uwasa.fi Sun Dec 26 00:00:07 1993
  324. Subject: Primers to interrupt programming
  325.  
  326. 7. *****
  327.  Q: What are interrupts and how to use them in Turbo Pascal?
  328.  
  329.  A: An interrupt is a signal to the processor from a program, a
  330. hardware device, or the processor itself, to suspend temporarily
  331. what the program is doing, and to perform a routine that is stored
  332. in the operating system. There are 256 such interrupt routines, with
  333. many subservices stored in memory at locations, which are given in
  334. the so called interrupt table. Turbo Pascal (somewhat like C) has a
  335. special keyword Intr, and a predefined variable registers (in the
  336. Dos unit) to access these interrupt routines. One way of looking at
  337. them is as Turbo Pascal (complicated lowlevel) subroutines that are
  338. already there ready for you to call.
  339.    A detailed description of interrupt routines is way beyond a
  340. single message with limited space. Instead, I shall give a simple
  341. example, and good references to the subject. (For a somewhat more
  342. comprehensive description of what an interrupt is, see INTERRUP.PRI
  343. in Ralf Brown's garbo.uwasa.fi:/pc/programming/inter38b.zip.)
  344.      :
  345.      uses Dos;
  346.      (* This procedure turns on the border color for CGA and VGA *)
  347.      procedure BORDER (color : byte);
  348.      var regs : registers;  (* Predeclared in the Dos unit *)
  349.      begin
  350.        FillChar (regs, SizeOf(regs), 0);  (* A precaution *)
  351.        regs.ax := $0B00;    (* Service number *)
  352.        regs.bh := $00;      (* Subservice number *)
  353.        regs.bl := color;
  354.        Intr ($10, regs);    (* ROM BIOS video driver interrupt *)
  355.      end;  (* border *)
  356.    If you are new the subject and / or want ideas on the most useful
  357. interrupts in Turbo Pascal programming, Ben Ezzel (1989),
  358. Programming the IBM User Interface Using Turbo Pascal, is definitely
  359. the best reference to look at. There are also many other good
  360. references for a novice interrupt user, such as Jamsa & Nameroff,
  361. Turbo Pascal Programmer's Library.
  362.    If you are a more advanced interrupt user you'll find the
  363. following references very useful. Michael Tischer (1990), Turbo
  364. Pascal Internals; Norton & Wilton (1988), The New Peter Norton
  365. Programmer's guide to the IBM PC & PS/2; Ray Duncan (1988), Advanced
  366. MS-DOS Programming; Terry Dettmann (1989), Dos Programmer's
  367. Reference, Second edition, Que. Furthermore, there is an impressive
  368. list of interrupts collected and maintained by Ralf Brown. His
  369. extensive /pc/programming/inter38a.zip, inter38b.zip, inter38c.zip
  370. and inter38d.zip (or whatever are the current versions when you read
  371. this) is available by anonymous FTP or mail server from
  372. garbo.uwasa.fi. A definite must for an advanced user. Also see the
  373. reference to Brown's and Kyle's book in the bibliography at the end
  374. of this FAQ. Another useful (but in many respects a replicate) list
  375. is contained in the file /pc/programming/dosref22.zip (or, sigh,
  376. whatever is the current version). There is also a good hypertext
  377. advanced programmer's quick reference /pc/programming/helppc21.zip
  378. which you might find useful.
  379.    One more point for Turbo Pascal users. When Borland upgraded from
  380. version 3 to 4.0 quite a number of tasks that needed to be done
  381. using interrupts (such as getting the current time) were included as
  382. normal TP routines. This means that while definitely useful,
  383. interrupt programming is now relevant only in advanced Turbo Pascal
  384. programming. Turbo Pascal 5.0 introduced a few more, but you can
  385. find some of the missing TP 4.0 routines in the compatibility unit
  386. in my garbo.uwasa.fi:/pc/ts/tspa3340.zip TP units collection.
  387. --------------------------------------------------------------------
  388.  
  389. From ts@uwasa.fi Sun Dec 26 00:00:08 1993
  390. Subject: Borland's Turbo Pascal upgrades
  391.  
  392. 8. *****
  393.  Q: Should I upgrade my Turbo Pascal version?
  394.  
  395.  A1: Depends on what version you are using, and for what purposes.
  396. If you are using version 3, the answer is a definite yes. There are
  397. so many useful additions in the later version, including the concept
  398. of units, and a great number of new useful keywords. The only reason
  399. that I can think of for using TP 3 is that it makes .com files
  400. (which reside in one memory segment only) instead of .exe files. As
  401. an accounting and business finance teacher and researcher I've been
  402. somewhat surprised to see postings stating that some users still
  403. have to program in TP 3.0 because their employer doesn't want to
  404. take the cost of upgrading. I find this cost argument ridiculous.
  405. How about some consideration for cost effectiveness and
  406. productivity?
  407.    If you are currently using version 4.0, the most important point
  408. in considering upgrading is the integrated debugger in the later
  409. versions. It is really good, and useful if you write much code.
  410. There are some minor considerations, as well. Later versions contain
  411. some useful routines which 4.0 does not. I have programmed many of
  412. them to be available also for 4.0 in my /pc/ts/tspa3340.zip TO units
  413. collection (or whatever is the latest when you read this).
  414. Furthermore, I find somewhat annoying that the executables will
  415. always end up in the default directory.
  416.    If you are currently using version 5.0 the rational reasons for
  417. upgrading are needing objects, and a better overlay manager. I have
  418. also version 5.5 myself, but switched back to version 5.0 after I
  419. had some problems with its linking of object files. (This is a false
  420. statement from me, since it turned out that I had made a mistake
  421. myself. My thanks are due to bj_stedm@gould2.bristol-poly.ac.uk
  422. (Bruce Stedman) for questioning this item). Anyway, I don't use nor
  423. need OOP objects (don't confuse linking object files and object
  424. oriented programming here). One further point for 5.5. It has a
  425. better help function than 5.0, and a few more procedures and
  426. predefined constants. The TP 5.5 help includes examples, which can
  427. be even pasted into your program. This is handy.
  428.    The real snag in upgrading (waiving the reasonable cost) is the
  429. fact that the units of the different versions are incompatible. If
  430. you have a large library of units (as I do) you will have to
  431. recompile the lot. This is something that has caused a fair amount
  432. of justifiable flak against an otherwise excellent product.
  433.    A tip. Don't throw away your Turbo Pascal version 3.0 manual, if
  434. you have one. It is of use if you resort to the Turbo3 and Graph3
  435. compatibility units. They give you access e.g. to turtle graphics.
  436.    At the time of first writing this Turbo Pascal 6.0 version had
  437. just been announced. I didn't have it yet myself, but I had been
  438. (correctly) informed that its units are not compatible with the
  439. earlier versions. I now have Turbo Pascal 6.0, and I must say that
  440. my reactions have been disappointment and frustration. This is
  441. probably partly (but not entirely) my own fault, since Turbo Pascal
  442. seems to be headed from a common programming language into a full
  443. professional's specialized tool, with many features I don't know how
  444. to utilize. The only advancement from my point of view really is the
  445. multiple file editing, but I have long had alternative programs for
  446. that. If I used assembler (I don't) I am sure that I would find
  447. useful TP 6.0's potential to include assembler code as such instead
  448. of having to use the cumbersome Inline procedure of entering the
  449. assembler code.
  450.    There is also a Windows Turbo Pascal, as the latest addition to
  451. the plethora. Since I don't use Windows at all, I have no futher
  452. information on it.
  453.    I think a pattern is emerging here. Rather than being different
  454. versions of the same product, the consecutive Turbo Pascals are
  455. really different products for different purposes. Version 3.0 was a
  456. simple programming language. Version 4.0 extended it into a full
  457. scale programming modular platform. Version 5.0 introduced the
  458. debugger. And there an advanced hobbyist's path ended. Version 5.5
  459. introduced object oriented programming, which I'm sure is important
  460. for the initiated, but personally I just don't need it even if I
  461. write a lot of programs. And with the 6.0 we go completely out of
  462. the realm of conventional programming into Turbo Pascal visions.
  463. And Windows Turbo Pascal is for a different platform, altogether.
  464.    I find the new integrated user interface of TP 6.0 awkward in
  465. comparison to what was used in the 4.0, 5.0, and 5.5 versions. The
  466. IDE of TP leaves less free memory than the previous versions.
  467. Furthermore TP 6.0 IDE performs frequent disk accesses which cause
  468. slowdowns  making it virtually unusable from a floppy. And I
  469. wonder why Borland didn't at once go all the way to Windows, because
  470. that is what 6.0 really is. An intermediate, incomplete step in that
  471. direction. This means that we have a 5th upgrade in line with
  472. incompatible units. This is aggravating even for a TP fan, isn't it?
  473.    For information on Turbo Pascal version 7.0 and Borland email
  474. contact numbers see garbo.uwasa.fi:/pc/turbopas/bp7-info.zip. Also
  475. see bp7bugs*.zip by Duncan Murdoch. Turbo Pascal 7.0 or more
  476. extensively Borland Pascal 7.0 is a full professional's tool, and
  477. far beyond for example my moderate programming needs. To list only a
  478. few of the features are protected mode programming, handling of
  479. lagre programs, very fast compiling, and a daunting amount of
  480. matrial elbowing its away on one's disk space if one ever has the
  481. patience to look through it all. I would use the word
  482. "overwhelming". But for a serious programmer this is an impressive
  483. and a very worthwhile tool. One should not be misled skipping it
  484. because of my comments which were written from a hobbyist's point of
  485. view. As a general trend in programs, the well-known columnist John
  486. C. Dvorak calls this increasing product complexity trend "featurism"
  487. in PC Computing, May 1993. But TP 7.0 (7.01) has some important
  488. features also from a hobbyist's point of view. So much so that I
  489. have finally succumbed to 7.01 myself. I particularly like the color
  490. coding of the keywords, and the TPX version enabling compiling very
  491. large programs (utilizing extended memory). A very welcome addition
  492. are the new keywords break and continue to exit or recycle a for,
  493. while or a repeat loop are. Besides they make using the outcast goto
  494. statements virtually unnecessary.
  495.  
  496.  A2: From: dmurdoch@watstat.waterloo.edu (Duncan Murdoch),
  497. Newsgroups: comp.lang.pascal. Included with Duncan's kind
  498. permission. (Duncan is one of the most knowledgeable and useful
  499. contributors to the comp.lang.pascal UseNet newsgroup).
  500.    One other reason:  there's a bug in the code generator for 4.0
  501. and 5.0 that makes it handle the Extended (10 byte) real type
  502. poorly.  The code generated makes very poor use of the 8 element
  503. internal stack on the coprocessor, so that expressions with lots of
  504. operands like
  505.   e1+e2+e3+e4+e5+e6+e7+e8+e9
  506. always fail, if all the e's are of type extended.  (The generated
  507. code pushes each operand onto the stack, then does all the adds.
  508. It's smarter to push and add them one at a time.)
  509.    This makes it a real pain translating numerical routines from
  510. Fortran, especially since constants are taken to be of type
  511. extended.
  512.    The bug was fixed in 5.5.
  513.  
  514.  A3: From: Bengt Oehman (d92bo@efd.lth.se): A difference between
  515. v4.0 and v5.5 is that you can calculate constants in tp55, but not
  516. in 4.0. I see this as a big advantage. For example:
  517.   CONST MaxW = 10;
  518.         MaxH = 20;
  519.         MaxSize = MaxW*MaxH;
  520.         { or }
  521.         MaxX = 100;
  522.         HalfMaxX = MaxX DIV 2;
  523. cannot be compiled with 4.0.
  524. --------------------------------------------------------------------
  525.  
  526. From ts@uwasa.fi Sun Dec 26 00:00:09 1993
  527. Subject: Shelling from a TP program
  528.  
  529. 9. *****
  530.  Q: How do I execute an MsDos command from within a TP program?
  531.  
  532.  A: The best way to answer this question is to give an example.
  533.      {$M 2048, 0, 0}   (* <-- Important *)
  534.      program outside;
  535.      uses dos;
  536.      begin
  537.        write ('Directory call from within TP by Timo Salmi');
  538.        SwapVectors;
  539.        Exec (GetEnv('comspec'), '/c dir *.*');  (* Execution *)
  540.        SwapVectors;
  541.        (* Testing for errors is recommended *)
  542.        if DosError <> 0 then
  543.          writeln ('Dos error number ', DosError)
  544.        else
  545.          writeln ('Mission accomplished, exit code ', DosExitCode);
  546.        (* For DosError and DosExitCode details see the TP manual *)
  547.      end.
  548. Alternatively, take a look at execdemo.pas from demos.arc which
  549. should be on the disk accompanying Turbo Pascal.
  550.    What the above Exec does is that it executes the command
  551. processor. The /c specifies that the command interpreter is to
  552. perform the command, and then stop (not halt).
  553.    I have also seen it asked how one can swap the Turbo Pascal
  554. program to the disk when shelling. It is unnecessary to program that
  555. separately because there is an excellent program to do that for you.
  556. It is garbo.uwasa.fi:/pc/sysutil/shroom2d.zip.
  557. --------------------------------------------------------------------
  558.  
  559. From ts@uwasa.fi Sun Dec 26 00:00:10 1993
  560. Subject: Millisecond timing
  561.  
  562. 10. *****
  563.  Q: How is millisecond timing done?
  564.  
  565.  A: A difficult task, but the facilities are readily available.
  566. TurboPower Software's commercial Turbo Professional (don't confuse
  567. with Borland's Turbo Professional) has a unit for this. (The usual
  568. disclaimer applies). This one has been released to the PD. It is
  569. called tptimer and is part of the /pc/turbopas/bonus507.zip package.
  570. I have also seen a SimTel upload announcement of a ztimer11.zip for
  571. C and ASM, but I have no further information on that. Another option
  572. is /pc/turbopas/qwktimer.zip. It is not quite as accurate as
  573. tptimer.
  574.    To test the tptimer unit in bonus507.zip you can use the
  575. following example code
  576.   uses Crt, tptimer;
  577.   var time1, time2 : longint;
  578.   begin
  579.     InitializeTimer;
  580.     time1 := ReadTimer;
  581.     Delay (1356);    (* Or whatever code you wish to benchmark *)
  582.     time2 := ReadTimer;
  583.     RestoreTimer;
  584.     writeln ('Elapsed = ', ElapsedTime (time1, time2)/1000.0 : 0 : 3);
  585.   end.
  586.    It is quite another question when you really need the millisecond
  587. timing. The most common purpose for millisecond timing is testing
  588. the efficiency of alternative procedures and functions, right? The
  589. way I compare mine is simple. I call the procedures or functions I
  590. want to compare for speed, say, a thousand times in a loop.  And
  591. test this for elapsed time. This way the normal resolution (18.2
  592. cycles per second) of the system clock becomes sufficient. This is
  593. accurate enough for the comparisons.
  594.      var elapsed : real; i : word;
  595.      elapsed := TIMERFN;  (* e.g. from /pc/ts/tspa3355.zip *)
  596.      for i := 1 to 1000 do YOURTEST;  (* Try out the alternatives *)
  597.      elapsed := TIMERFN - elapsed;
  598.      writeln ('Elapsed : ', elapsed : 0 : 2);
  599. Incidentally, if you want to make more elaborate evaluations of the
  600. efficiency of your code, Borland's Turbo Profiler is a useful tool.
  601. (The usual disclaimer naturally applies.)
  602. --------------------------------------------------------------------
  603.  
  604. From ts@uwasa.fi Sun Dec 26 00:00:11 1993
  605. Subject: Text font customizing
  606.  
  607. 11. *****
  608.  Q: How can I customize the text characters to my own liking?
  609.  
  610.  A: As far as I know, text-mode characters are hard-coded, and
  611. cannot be customized at all unless you have an EGA or VGA adapter.
  612. But you can always retrieve the bitmap information for the ascii
  613. characters from your PC.
  614.    The bitmap table for the lower part of the character set (0-127)
  615. starts at memory position $F000 and ends at $FA6E. The upper part is
  616. not at a fixed memory location. The pointer to the memory address of
  617. upper part of the ascii table (provided that graftabl has been
  618. loaded) is at an address $007C. One way of saying this is that the
  619. segment address of the upper part's memory location is at $007E, and
  620. its offset at $007C.
  621.    Going into more details is beyond the scope of this posting. If
  622. you want more information see Michael Ticher (1992), PC Intern
  623. System Programming, "Selecting and Programming Fonts", pp. 197-210.
  624. It also has information on a remotely related task of using sprites
  625. (pp. 305-373), a concept familiar from the days of the Commodore 64
  626. games programming. For another reference to customizing characters
  627. see Kent Porter (1987), Stretching Turbo Pascal, Chapter 12, and
  628. Kent Porter & Mike Floyd (1990), Stretching Turbo Pascal. Version
  629. 5.5. Revised Edition. Brady, Chapter 11.
  630.    If you are interested in a demonstration of utilizing the
  631. bitmapped character information (no source code available), take a
  632. look at the demo in the garbo.uwasa.fi anonymous FTP archives file
  633. /pc/ts/tsdemo16.zip (or whatever version number is current).
  634.    Turbo Pascal also supports what is called stroked fonts (the
  635. .chr) files which draw characters instead of bitmapping them. The
  636. user should be able to write one's own .chr definitions, but I have
  637. no experience nor information on how this can be done.
  638.    There is something called bgikit10.zip which has facilities for
  639. making fonts and adding graphics drivers. The problem is that I
  640. cannot make it publicly available, since I think that it is not PD.
  641. I am still missing the information. Unfortunately, it is not even
  642. the only case where I encountered the fact that Borland does not
  643. seem at all interested in the UseNet users' queries about the status
  644. and distributability of their material.
  645.    (From Leonard Erickson Leonard.Erickson@f51.n105.z1.fidonet.org
  646. Well, you can *also* do it if you have a Hercules Graphics Card Plus
  647. or Hercules InColor card (as far as I know, the only cards that
  648. implemented Hercules RamFont 'standard'). And you can modify the
  649. upper 128 characters on a CGA card. BTW, the RamFont cards are
  650. *nice* pity it appeared too late to become a standard. It's a *lot*
  651. more flexible than EGA/VGA fonts (I can have several *dozen* fonts
  652. resident).)
  653. --------------------------------------------------------------------
  654.  
  655. From ts@uwasa.fi Sun Dec 26 00:00:12 1993
  656. Subject: Finding files in TP
  657.  
  658. 12. *****
  659.  Q: How to find the files in a directory AND subdirectories?
  660.  
  661.  A: Writing a program that goes through the files of the directory,
  662. and all the subdirectories below it, is based on Turbo Pascal's file
  663. finding commands and recursion. This is universal whether you are
  664. writing, for example, a directory listing program, or a program that
  665. deletes, say, all the .bak files, or some other similar task.
  666.    To find (for listing or other purposes) the files in a directory
  667. you need above all the FindFirst and FindNext keywords, and testing
  668. the predefined file attributes. You make these a procedure, and call
  669. it recursively. If you want good examples with source code, please
  670. see PC World, April 1989, p. 154; Kent Porter & Mike Floyd (1990),
  671. Stretching Turbo Pascal. Version 5.5. Revised Edition, Chapter 23;
  672. Michael Yester (1989), Using Turbo Pascal, p. 437; Michael Ticher
  673. (1992), PC Intern System Programming, pp. 796-798; dirtree.pas in
  674. /pc/pcmag/vol8n01.zip.
  675.    The simple (non-recursive) example listing all the read-only
  676. files in the current directory shows the rudiments of the principle
  677. of Using FindFirst, FindNext, and the file attributes, because some
  678. users find it hard to use these keywords. Also see the code in the
  679. item "How to establish if a name refers to a directory or not?" of
  680. this same FAQ collection you are now reading.
  681.     uses Dos;
  682.     var FileInfo : SearchRec;
  683.     begin
  684.       FindFirst ('*.*', AnyFile, FileInfo);
  685.       while DosError = 0 do
  686.         begin
  687.           if (FileInfo.Attr and ReadOnly) > 0 then
  688.             writeln (FileInfo.Name);
  689.           FindNext (FileInfo);
  690.         end;
  691.     end.  (* test *)
  692.  
  693.  A2: While we are on the subject related to FindFirst and FindNext,
  694. here are two useful examples:
  695.  
  696. (* Number of files in a directory (not counting directories) *)
  697. function DFILESFN (dirName : string) : word;
  698. var nberOfFiles  : word;
  699.     FileInfo     : searchRec;
  700. begin
  701.   if dirName[Length(dirName)] <> '\' then dirName := dirName + '\';
  702.   dirName := dirName + '*.*';
  703.   {}
  704.   nberOfFiles := 0;
  705.   FindFirst (dirName, AnyFile, FileInfo);
  706.   while DosError = 0 do
  707.     begin
  708.       if ((FileInfo.Attr and VolumeId) = 0) then
  709.         if (FileInfo.Attr and Directory) = 0 then
  710.           Inc (nberOfFiles);
  711.       FindNext (FileInfo);
  712.     end; {while}
  713.   dfilesfn := nberOfFiles;
  714. end;  (* dfilesfn *)
  715.  
  716. (* Number of immediate subdirectories in a directory *)
  717. function DDIRSFN (dirName : string) : word;
  718. var nberOfDirs : word;
  719.     FileInfo    : searchRec;
  720. begin
  721.   if dirName[Length(dirName)] <> '\' then dirName := dirName + '\';
  722.   dirName := dirName + '*.*';
  723.   {}
  724.   nberOfDirs:= 0;
  725.   FindFirst (dirName, AnyFile, FileInfo);
  726.   while DosError = 0 do
  727.     begin
  728.       if ((FileInfo.Attr and VolumeId) = 0) then
  729.         if (FileInfo.Attr and Directory) > 0 then
  730.           if (FileInfo.Name <> '.') and (FileInfo.Name <> '..') then
  731.             Inc (nberOfDirs);
  732.       FindNext (FileInfo);
  733.     end; {while}
  734.   ddirsfn := nberOfDirs;
  735. end;  (* ddirsfn *)
  736. --------------------------------------------------------------------
  737.  
  738. From ts@uwasa.fi Sun Dec 26 00:00:13 1993
  739. Subject: A generic power function code for TP
  740.  
  741. 13. *****
  742.  Q: I need a power function but there is none in Turbo Pascal.
  743.  
  744.  A: Pascals do not have an inbuilt power function. You have to write
  745. one yourself. The common, but non-general method is defining
  746.    function POWERFN (number, exponent : real) : real;
  747.      begin
  748.        powerfn := Exp(exponent*Ln(number));
  749.      end;
  750. To make it general use:
  751.    (* Generalized power function by Prof. Timo Salmi *)
  752.    function GENPOWFN (number, exponent : real) : real;
  753.    begin
  754.      if (exponent = 0.0) then
  755.        genpowfn := 1.0
  756.      else if number = 0.0 then
  757.        genpowfn := 0.0
  758.      else if abs(exponent*Ln(abs(number))) > 87.498 then
  759.        begin writeln ('Overflow in GENPOWFN expression'); halt; end
  760.      else if number > 0.0 then
  761.        genpowfn := Exp(exponent*Ln(number))
  762.      else if (number < 0.0) and (Frac(exponent) = 0.0) then
  763.        if Odd(Round(exponent)) then
  764.          genpowfn := -GENPOWFN (-number, exponent)
  765.        else
  766.          genpowfn :=  GENPOWFN (-number, exponent)
  767.      else
  768.        begin writeln ('Invalid GENPOWFN expression'); halt; end;
  769.    end;  (* genpowfn *)
  770. On the lighter side of things an extract from an answer of mine in
  771. the comp.lang.pascal UseNet newsgroup:
  772.  >anyone point out why X**Y is not allowed in Turbo Pascal?
  773.    The situation in TP is a left-over from standard
  774.    Pascal. You'll recall that Pascal was originally
  775.    devised for teaching programming, not for
  776.    something as silly and frivolous as actually
  777.    writing programs.  :-)
  778. --------------------------------------------------------------------
  779.  
  780. From ts@uwasa.fi Sun Dec 26 00:00:14 1993
  781. Subject: Arrays > 64K
  782.  
  783. 14. *****
  784.  Q: How can I create arrays that are larger than 64 kilobytes?
  785.  
  786.  A: Turbo Pascal does not directly support the so-called huge arrays
  787. but you can get by this problem with a clever use of pointers as
  788. presented in Kent Porter, "Handling Huge Arrays", Dr.Dobb's Journal,
  789. March 1988. In this method you point to an element of a two
  790. dimensional array using a^[row].col^[column]. The idea involves too
  791. much code and explanation to be repeated here, so you'll have to see
  792. the original reference. But I know from my own experience, that the
  793. code works like magic. (The code is available from garbo.uwasa.fi
  794. archives as /pc/turbopas/ddj8803.zip). Kent Porter, "Huge Arrays
  795. Revisited", Dr.Dobb's Journal, October 1988, presents the extension
  796. of the idea to huge virtual arrays. (Virtual arrays mean arrays that
  797. utilize disk space).
  798.    Another possibility is using TurboPower Software's (the usual
  799. disclaimer applies) commercial Turbo Professional (don't confuse
  800. with Borland's Turbo Professional) package. It has facilities for
  801. huge arrays, but they involve much more overhead than Kent Porter's
  802. excellent method.
  803.  
  804. (* =================================================================
  805.    My code below is based on a UseNet posting in comp.lang.pascal
  806.    by Naji Mouawad nmouawad@watmath.waterloo.edu. Naji's idea was
  807.    for a vector, my adaptation is for a two-dimensional matrix. The
  808.    realization of the idea is simpler than the one presented by Kent
  809.    Porter in Dr.Dobb's Journal, March 1988. (Is something wrong,
  810.    this is experimental.)
  811.    ================================================================= *)
  812.    {}
  813.    const maxm = 150;
  814.          maxn = 250;
  815.    {}
  816.    type BigVectorType = array [1..maxn] of real;
  817.         BigMatrixType = array [1..maxm] of ^BigVectorType;
  818.    {}
  819.    var BigAPtr : BigMatrixType;
  820.    {}
  821.    (* Create the dynamic variables *)
  822.    procedure MAKEBIG;
  823.    var i          : word;
  824.        heapNeeded : longint;
  825.    begin
  826.      heapNeeded := maxm * maxn * SizeOf(real) + maxm * 4 + 8196;
  827.      if (MaxAvail <= heapNeeded) then
  828.        begin writeln ('Out of memory'); halt; end;
  829.      for i := 1 to maxm do New (BigAPtr[i]);
  830.    end;  (* makebig *)
  831.    {}
  832.    (* Test that it works *)
  833.    procedure TEST;
  834.    var i, j : word;
  835.    begin
  836.      for i := 1 to maxm do
  837.        for j := 1 to maxn do
  838.          BigAPtr[i]^[j] := i * j;
  839.      {}
  840.      writeln (BigAPtr[5]^[7] : 0:0);
  841.      writeln (BigAPtr[maxm]^[maxn] : 0:0);
  842.    end;  (* test *)
  843.    {}
  844.    (* The main program *)
  845.    begin
  846.      writeln ('Big arrays test by Prof. Timo Salmi, Vaasa, Finland');
  847.      writeln;
  848.      MAKEBIG;
  849.      TEST;
  850.    end.
  851. (For a better test of the heap than in MAKEBIG see Swan (1989), pp.
  852. 462-463.)
  853. --------------------------------------------------------------------
  854.  
  855. From ts@uwasa.fi Sun Dec 26 00:00:15 1993
  856. Subject: Testing printer status
  857.  
  858. 15. *****
  859.  Q: How can I test that the printer is ready?
  860.  
  861.  A: The usually advocated method in Turbo Pascal is to test the
  862. status of regs.ah after a call to interrupt 17 Hex (the parallel
  863. port driver interrupt), service 02:
  864.       regs.dx := PrinterNumber;  (* LPT1 = 0 *)
  865.       regs.ah := $02;            (* var regs : registers, uses DOS *)
  866.       Intr ($17,regs);           (* Interrupt 17 Hex *)
  867.       status := regs.ah          (* var status : byte *)
  868. But this is not a good method since the combinations of the status
  869. bits which indicate a ready state can vary from printer to printer
  870. and PC to PC. If you want a list of the status bits, see eg Ray
  871. Duncan (1988), Advanced MS-DOS Programming, p. 587. For an example
  872. of a code using interrupt 17 Hex see Douglas Stivison (1986), Turbo
  873. Pascal Library, pp. 118-120. Also see Michael Yester (1989), Using
  874. Turbo Pascal, pp. 494-495.
  875.    The more generic alternative is to try to write a #13 to the
  876. printer having the i/o checking off, that is, while {$I-} is in
  877. effect, and testing the IOResult. But then you must first alter the
  878. printer retry times default (and restore it afterwards). Else the
  879. method can take up to a minute instead of an immediate response.
  880. Also, you must have set the FileMode for LPT1 appropriately (and
  881. restore it afterwards). Sounds a bit complicated, but you don't have
  882. to do all this yourself. There is a boolean function "LPTONLFN Get
  883. the online status of the first parallel printer" for this purpose in
  884. my /pc/ts/tspa33??.zip (or whatever version number is the latest)
  885. Turbo Pascal units collection available by anonymous FTP or mail
  886. server from garbo.uwasa.fi.
  887. --------------------------------------------------------------------
  888.  
  889. From ts@uwasa.fi Sun Dec 26 00:00:16 1993
  890. Subject: Clearing the keyboard buffer
  891.  
  892. 16. *****
  893.  Q: How can I clear the keyboard type-ahead buffer?
  894.  
  895.  A: Three methods are usually suggested for solving this problem.
  896. a) The first is to use something like
  897.      uses Crt;
  898.      var dummy : char;
  899.      while KeyPressed do dummy := ReadKey;
  900. This kludge-type method has the disadvantage of requiring the Crt
  901. unit. Also, in connection with procedures relying on ReadKey for
  902. input, it may cause havoc on the programs logic.
  903. b) The second method accesses directly the circular keyboard buffer
  904.      var head : word absolute $0040:$001A;
  905.          tail : word absolute $0040:$001C;
  906.      procedure FLUSHKB; begin head := tail; end;
  907. For a slightly different formulation of the same method see Michael
  908. Ticher (1992), PC Intern System Programming, p. 462.
  909. c) The third method is to call interrupt 21Hex (the MsDos interrupt)
  910. with the ax register set as $0C00. This method has the advantage of
  911. not being "hard-coded" like the second method, and thus should be
  912. less prone to incompatibility.
  913. --------------------------------------------------------------------
  914.  
  915. From ts@uwasa.fi Sun Dec 26 00:00:17 1993
  916. Subject: Utilizing expanded memory
  917.  
  918. 17. *****
  919.  Q: How can I utilize expanded memory (EMS) in my programs?
  920.  
  921.  A: I have no experience (yet?) on this subject myself, but I can
  922. give you a list of references: Michael Tischer (1990), Turbo Pascal
  923. Internals, Abacus, Chapter 9; Michael Ticher (1992), PC Intern
  924. System Programming, Chapter 12; Stephen O'Brien (1988), Turbo
  925. Pascal, Advanced Programmer's Guide, Borland-Osborne, Chapter 4;
  926. Chris Ohlsen & Gary Stoker (1989), Turbo Pascal Advanced Techniques,
  927. Que, Chapter 11, and, maybe most importantly, Dorfman & Neuberger,
  928. Turbo Pascal Memory Management Techniques (with lots of code).
  929.    Furthermore, Turbo Pascal delivery disks (at least 5.0) contain a
  930. demos.arc archive which includes an ems.pas file.
  931. --------------------------------------------------------------------
  932.  
  933. From ts@uwasa.fi Sun Dec 26 00:00:18 1993
  934. Subject: Capturing the entire command line
  935.  
  936. 18. *****
  937.  Q: How can I obtain the entire command line (spaces and all)?
  938.  
  939.  A: ParamCount and ParamStr are for parsed parts of the command line
  940. and cannot be used to get the command line exactly as it was. See
  941. what happens if you try to capture
  942.   "Hello.   I'm here"
  943. you'll end up with a false number of blanks. For obtaining the
  944. command line unaltered use
  945.   type CommandLineType = string[127];
  946.   var  CommandLinePtr  : ^CommandLineType;
  947.   begin
  948.     CommandLinePtr := Ptr(PrefixSeg, $80);
  949.     writeln (CommandLinePtr^);
  950.   end;
  951. A warning. If you want to get this correct (the same goes for TP's
  952. own ParamStr and ParamCount) apply them early in your program. At
  953. least they must be used before any disk I/O takes place!
  954. :
  955. A related example demonstrating a function giving the number of
  956. characters on the command line
  957.   function CMDNBRFN : byte;
  958.   var paramPtr : ^byte;
  959.   begin
  960.     paramPtr := Ptr (PrefixSeg, $80);
  961.     cmdnbrfn := paramPtr^
  962.   end;  (* cmdnbrfn *)
  963. For the contents of the Program Segment Prefix (PSP) see Tischer,
  964. Michael (1992), PC Intern System Programming, p. 753.
  965. --------------------------------------------------------------------
  966.  
  967. From ts@uwasa.fi Sun Dec 26 00:00:19 1993
  968. Subject: Redirecting from printer to file
  969.  
  970. 19. *****
  971.  Q: How do I redirect text from printer to file in my TP program?
  972.  
  973.  A: Simple. This is done in Turbo Pascal by using the assign command
  974. (think what the word 'assign' implies). Here is a simple example of
  975. the idea.
  976.   uses Printer;
  977.   begin
  978.     assign (lst, 'printer.log');
  979.     rewrite (lst);
  980.     writeln (lst, 'Hello world');
  981.     close (lst);
  982.   end.
  983. --------------------------------------------------------------------
  984.  
  985. From ts@uwasa.fi Sun Dec 26 00:00:20 1993
  986. Subject: Turbo Pascal users are just wimps
  987.  
  988. 20. *****
  989.  Q: Turbo Pascal is for wimps. Why don't you use standard Pascal or
  990. better still why don't you use C?
  991.  
  992.  A: These kinds of "real-programmers" statements often reflect what
  993. is called self-over-others attitude, and they are a part of a kind
  994. of a programming lore or cult. Basically, these attitudes waive the
  995. simple fact that one should select one's tools according to the task
  996. at hand, not vice versa. On the other hand one's productivity is
  997. usually best when being able to use tools which one is familiar and
  998. comfortable with. (Note however that the real-programmer's lore is
  999. not really interested in producing results.)
  1000.    In very rough terms there are two attitudes to programming
  1001. languages. They can be seen as tools for writing applications, or
  1002. (by surprisingly many) as ends themselves.
  1003.    If we first look at standard Pascal (versus Turbo Pascal),
  1004. considering the language primary and its usage secondary is common.
  1005. This results from the history of Pascal, since as we all know it was
  1006. originally meant as a means for teaching programming concepts, not
  1007. at all for writing applications. But because Pascal turned out to be
  1008. useful also for writing applications, it has been extended for some
  1009. operating systems, most notably MsDos (Turbo Pascal) and VAX/VMS
  1010. (VAX Pascal). Both remedy a lot of flaws from the application
  1011. programmer's point of view. Most importantly they have a true file
  1012. I/O interface, and enhanced string handling. Turbo Pascal (the more
  1013. generic of these two) clearly draws from the structure and ideas of
  1014. advanced BASICs (and vice versa). While in standard Pascal the
  1015. language is an end by itself, for Turbo Pascal the only relevant
  1016. issue is its usefulness for writing applications.
  1017.    One problem that one encounters when moving away from standard
  1018. Pascal is the problem of portability. This is a truly serious
  1019. problem, since most often extensive rewriting is necessary from
  1020. converting say a Turbo Pascal to, say, Unix Pascal. I have taken
  1021. Unix Pascal as the extreme example, since Unix Pascal in almost
  1022. nothing but the standard Pascal having no useful file I/O.
  1023.    If one considers C, its best aspect from applications point of
  1024. view is portability, and its strength for system programming. But it
  1025. is not an easy language to learn. Proponents of C also often have
  1026. the tendency discussed above, that is seeing the language as
  1027. primary, and its utilization as secondary. Now why this tendency,
  1028. not only for C, but in general? I've had the opportunity of writing
  1029. programs starting from the late 1960's, and one observation I have
  1030. made, and often propounded the view is that it is not writing code
  1031. that is the really difficult part. What is really difficult it is
  1032. coming up with good and original ideas for programs to write. I see
  1033. applications as primary, and the tools as secondary. As to Turbo
  1034. Pascal, I've written in many languages (including Cobol, Fortran,
  1035. several Basics and Pascals, and command languages) and I like Turbo
  1036. Pascal because it is one of the most convenient and flexible tools
  1037. for writing the kind of applications that I usually write and
  1038. distribute for the Public Domain. That is I use Turbo Pascal because
  1039. I'm comfortable with it in writing applications, and have thus
  1040. gathered a very useful modular library for it over the years, not
  1041. because of any inherent value attached to Turbo Pascal per se.
  1042.  
  1043.  A2: Another, a somewhat resembling line is made up by the arguments
  1044. about standards in Pascal which are recycled in comp.lang.pascal
  1045. time after time. Very often they end up with purists vs pragmatists
  1046. arguing about the true or imaginary viles of using GOTOs. I find all
  1047. this somewhat futile, although I understand the academic nature of
  1048. the background. As you'll recall, Pascal was first developed for
  1049. academic teaching programming concepts, not for any practical
  1050. programming. That came later, and the ensuing popularity of Pascal
  1051. in practical applications must have come as a surprise way back
  1052. then. I admit being biased in not symphatizing with Pascal standard
  1053. stalwarts. I am far more interested in getting the job done than in
  1054. defending a barren orthodoxy.
  1055. --------------------------------------------------------------------
  1056.  
  1057. From ts@uwasa.fi Sun Dec 26 00:00:21 1993
  1058. Subject: Turning off the cursor
  1059.  
  1060. 21. *****
  1061.  Q: How do I turn the cursor off?
  1062.  
  1063.  A: The usually advocated trick for turning the cursor off is to
  1064. equate the lower and the upper scan line of the cursor as explained
  1065. eg in Stephen O'Brien (1988), Turbo Pascal, Advanced Programmer's
  1066. Guide.
  1067.      uses Dos;
  1068.      var regs : registers;
  1069.      begin
  1070.        regs.ax := $0100;   (* Service $01 *)
  1071.        regs.cl := $20;     (* Top scan line *)
  1072.        regs.ch := $20;     (* Botton scan line *)
  1073.        Intr ($10, regs);   (* ROM BIOS video driver interrupt *)
  1074.      end;
  1075. To turn the cursor back on this (and many other) sources suggest
  1076. setting regs.ch and regs.cl as 12 and 13 for mono screen, and 6 and
  1077. 7 for others.
  1078.    This is not a good solution since it is equipment dependent, and
  1079. may thus produce unexcepted results. Better to store the current
  1080. scan line settings, and turn off the cursor bit. Below is the code
  1081. from my Turbo Pascal units collection /pc/ts/tspa33??.zip (or
  1082. whatever version number is the latest) available by anonymous FTP
  1083. from garbo.uwasa.fi archives. The general idea is that regs.ch bit 5
  1084. toggles the cursor on / off state. Thus to set the cursor off, apply
  1085.   regs.ch := regs.ch or $20;    (* $20 = 00100000 *)
  1086. and to set it on, apply
  1087.   regs.ch := regs.ch and $DF;   (* $DF = 11011111 *)
  1088. (* From TSUNTE unit, which also has a CURSON procedure *)
  1089. procedure CURSOFF;
  1090. var regs : registers;
  1091. begin
  1092.   FillChar (regs, SizeOf(regs), 0);  (* Initialize, a precaution *)
  1093.   {... find out the current cursor size (regs.ch, regs.cl) ...}
  1094.   regs.ah := $03;
  1095.   regs.bh := $00;    (* page 1, superfluous because of FillChar *)
  1096.   Intr ($10, regs);  (* ROM BIOS video driver interrupt *)
  1097.   {... turn off the cursor without changing its size ...}
  1098.   regs.ah := $01;                   (* Below are bits 76543210 *)
  1099.   regs.ch := regs.ch or $20;  (* Turn on bit 5; $20 = 00100000 *)
  1100.   Intr ($10, regs);
  1101. end;  (* cursoff *)
  1102.  
  1103.  A2: Another solution that has been suggested is putting the cursor
  1104. outside the screen using the GoToXY procedure. Fair enough, but then
  1105. you need to use the Crt unit, which is not always desirable.
  1106. Besides, how do you write on the screen if the cursor position is
  1107. off it?
  1108.  
  1109.  A3: (Not to be taken seriously). Simple, turn off your computer and
  1110. the cursor stops showing :-).
  1111. --------------------------------------------------------------------
  1112.  
  1113. From ts@uwasa.fi Sun Dec 26 00:00:22 1993
  1114. Subject: Finding the roots of a polynomial
  1115.  
  1116. 22. *****
  1117.  Q: How to find all roots of a polynomial?
  1118.  
  1119.  A: If you need the code, see Turbo Pascal Numerical Toolbox and/or
  1120. Press & Flannery & Teukolsky & Vetterling (1986), Numerical Recipes,
  1121. The Art of Scientific Computing, Cambridge University Press. The
  1122. Numerical Recipes codes are available as /pc/turbopas/nrpas13.zip
  1123. (big, 404k!). If you just need to solve such a task (without code
  1124. available), get /pc/ts/tsnum12.zip (or whatever version number is
  1125. the latest) from garbo.uwasa.fi archives by anonymous FTP or mail
  1126. server.
  1127. --------------------------------------------------------------------
  1128.  
  1129. From ts@uwasa.fi Sun Dec 26 00:00:23 1993
  1130. Subject: Pascal homework on the net
  1131.  
  1132. 23. *****
  1133.  Q: What is all this talk about "Pascal homework on the net"?
  1134.  
  1135.  A: This is one of the subjects that seems to pop up at regular
  1136. intervals, cause some heated exchange for awhile, and then die down
  1137. again leaving some users harboring warranted or unwarranted grudges.
  1138.    Some posters to comp.lang.pascal have been very concerned of the
  1139. possibility that the questions posed on the net are related to
  1140. students' homework assignments. I don't have any unequivocal answers
  1141. or a clearcut stand on this question, just some comments.
  1142.    The most important task of a newsgroup like comp.lang.pascal is
  1143. the exchange of information between the users. If you think that
  1144. what you are going to post is interesting and useful to the group,
  1145. that should be your topmost criterion.
  1146.    If it is really a student that wants his/her work done on the net
  1147. (how do we know anyway?) also consider the following fact. Being
  1148. able to use a newsgroup amounts to having learned at least something
  1149. about using computers, and that is something per se.
  1150.    Even if the student may short-sightedly not see it, providing ALL
  1151. the code for a student's homework is detrimental to the student,
  1152. since it is she/he that foregoes understanding what he/she is doing.
  1153. The group should not condone outright cheating. Being (partly) a
  1154. teacher myself, I understand also this view.
  1155.    If a student is stuck with a problem in his/her code, I don't see
  1156. any real harm in helping out, especially if the problem has general
  1157. interest. Instructing is what teaching is about, anyway, isn't it?
  1158.    But, on the other hand, I must admit that I find a it rather
  1159. flagrant if a posting asks for something of the kind "I have to
  1160. complete my term assignment to write a function plotter by the end
  1161. of this month. Send me the code, since I'm too busy with my other
  1162. exams to write it myself" (a true quote).
  1163.    Finally, let's not jump to premature conclusions about anyone's
  1164. questions.  That's what most often triggers off a vicious circle of
  1165. flaming.
  1166. --------------------------------------------------------------------
  1167.  
  1168. From ts@uwasa.fi Sun Dec 26 00:00:24 1993
  1169. Subject: Linking bgi drivers into executables
  1170.  
  1171. 24. *****
  1172.  Q: How can I link graphics drivers directly into my executable?
  1173.  
  1174.  A: This is a complicated, yet a very useful task, because then you
  1175. won't need any separate graphics drivers (or fonts) to go separately
  1176. along with your program. Unfortunately, Turbo Pascal documentation
  1177. on this task is a bit confusing.
  1178.    1) The very first step is to get the necessary files from the
  1179. Turbo Pascal disks to your working directory. To start with, you'll
  1180. need binobj.exe and all the .bgi files.
  1181.    2) Run the following commands (best to place them in a batch,
  1182. call it eg makeobj.bat):
  1183.      binobj cga.bgi cga CGADriverProc
  1184.      binobj egavga.bgi egavga EGAVGADriverProc
  1185.      binobj herc.bgi herc HercDriverProc
  1186.      binobj pc3270.bgi pc3270 PC3270DriverProc
  1187.      binobj att.bgi att ATTDriverProc
  1188.      rem binobj ibm8514.bgi 8514 IBM8514DriverProc
  1189.    3) Get drivers.pas from the Turbo Pascal disk and compile it with
  1190. Turbo Pascal. Now you have a drivers.tpu unit which contains all the
  1191. graphics drivers.
  1192.    4) Now you won't need the .bgi and the .obj files any more. You
  1193. may delete them from your working directory.
  1194.    5) Write your graphics program in the usual manner. But before
  1195. putting your program in the graphics mode use the following
  1196. procedure if you want to link e.g. the EGAVGA graphics driver
  1197. directly into your executable. (Link just the driver(s) you'll need,
  1198. since the drivers take up a lot of space.)
  1199.      uses Graph, Drivers;
  1200.      :
  1201.      procedure EGAVGA2EXE;
  1202.      begin
  1203.        if RegisterBGIdriver(@EGAVGADriverProc) < 0 then
  1204.          begin
  1205.            writeln ('EGA/VGA: ', GraphErrorMsg(GraphResult));
  1206.            halt(1);
  1207.          end;
  1208.      end; (* egavga2exe *)
  1209.      :
  1210.    Linking the .bgi and .chr drivers is also covered in Swan (1989),
  1211. Mastering Turbo Pascal 5.5 pp. 355-359 and Mitchell (1993), Borland
  1212. Pascal Developer's Guide , pp. 221-229.
  1213.    Incidentally, although this is a slightly different matter, you
  1214. can link any data material into your executable. See Stephen
  1215. O'Brien, (1988), Turbo Pascal, Advanced Programmer's Guide, pp. 31 -
  1216. 35 for more details.
  1217. --------------------------------------------------------------------
  1218.  
  1219. From ts@uwasa.fi Sun Dec 26 00:00:25 1993
  1220. Subject: Trapping runtime errors
  1221.  
  1222. 25. *****
  1223.  Q: How can I trap a runtime error?
  1224.  
  1225.  A: What you are probably asking for is a method writing a program
  1226. termination routine of your own. To do this, you have to replace
  1227. Turbo Pascal's ExitProc with your own customized exec procedure.
  1228. Several Turbo Pascal text books show ho to do this. See eg Tom Swan
  1229. (1989), Mastering Turbo Pascal 5.5, Third edition, Hayden Books, pp.
  1230. 440-454; Michael Yester (1989), Using Turbo Pascal, Que, pp.
  1231. 376-382; Stephen O'Brien (1988), Turbo Pascal, Advanced Programmer's
  1232. Guide, pp. 28-30; Tom Rugg & Phil Feldman (1989), Turbo Pascal
  1233. Programmer's Toolkit, Que, pp. 510-515. Here is an example
  1234.   var OldExitProcAddress : Pointer;
  1235.       x : real;
  1236.   {$F+} procedure MyExitProcedure; {$F-}
  1237.   begin
  1238.     if ErrorAddr <> nil then
  1239.       begin
  1240.         writeln ('Runtime error number ', ExitCode, ' has occurred');
  1241.         writeln ('The error address in decimal is ',
  1242.                   Seg(ErrorAddr^):5,':',Ofs(ErrorAddr^):5);
  1243.         writeln ('That''s all folks, bye bye');
  1244.         ErrorAddr := nil;
  1245.         ExitCode  := 0;
  1246.       end;
  1247.     {... Restore the pointer to the original exit procedure ...}
  1248.     ExitProc := OldExitProcAddress;
  1249.   end;  (* MyExitProcedure *)
  1250.   (* Main *)
  1251.   begin
  1252.     OldExitProcAddress := ExitProc;
  1253.     ExitProc := @MyExitProcedure;
  1254.     x := 7.0; writeln (1.0/x);
  1255.     x := 0.0; writeln (1.0/x);   {The trap}
  1256.     x := 7.0; writeln (4.0/x);   {We won't get this far}
  1257.   end.
  1258. :
  1259. Actually, I utilize this idea in my /pc/ts/tspa33??.zip Turbo Pascal
  1260. units collection, which includes a TSERR.TPU. If you put TSERR in
  1261. your program's uses statement, all the run time errors will be given
  1262. verbally besides the usual, cryptic error number. That's all there
  1263. is to it. That is, the inclusion to the uses statment to your main
  1264. program (if you have the program in several units) is all you have
  1265. to do to enable this handy feature.
  1266. --------------------------------------------------------------------
  1267.  
  1268. From ts@uwasa.fi Sun Dec 26 00:00:26 1993
  1269. Subject: Using ansi codes in a TP program
  1270.  
  1271. 26. *****
  1272.  Q: How to get ansi control codes working in Turbo Pascal writes?
  1273.  
  1274.  A: It is very simple, but one has to be aware of the pitfalls.
  1275. Let's start from the assumption that ansi.sys or a corresponding
  1276. driver has been loaded, and that you know ansi codes. If you don't,
  1277. you'll find that information in the standard MsDos manual. To apply
  1278. ansi codes you just include the ansi codes in your write statements.
  1279. For example the following first clears the screen and then puts the
  1280. text at location 10,10:
  1281.    write (#27, '[2J');         (* the ascii code for ESC is 27 *)
  1282.    write (#27, '[10;10HUsing ansi codes can be fun');
  1283. If you want to test (as you should) whether ansi.sys or some some
  1284. replacement driver has been loaded, you can use the ISANSIFN
  1285. function from my garbo.uwasa.fi:/pc/ts/tspa33??.zip.
  1286. Now the catches. If you have a
  1287.    uses Crt;
  1288. statement in your program, direct screen writes will be used, and
  1289. the ansi codes won't work. You have either to leave out the Crt
  1290. unit, or include
  1291.    assign (output, '');
  1292.    rewrite (output);
  1293.    :
  1294.    close (output);
  1295. Occasionally I have seen it suggested that one should just set
  1296.    DirectVideo := false;
  1297. This is a popular misconception. It won't produce the desired
  1298. result. I'm not claiming to know the reason for this quirk of Turbo
  1299. Pascal. Rather it is an observation I've made.
  1300.  
  1301. -From: Bengt Oehman (d92bo@efd.lth.se)
  1302. The `DirectVideo:=False' statement only tells the Crt unit to use
  1303. BIOS calls instead of using direct video-memory writes. A demo
  1304. program to illustrate the screen writing modes follows:
  1305.  
  1306. Program ScreenWriteDemo;
  1307. USES Crt;
  1308. BEGIN
  1309.   Writeln('This is written directly to the video memory');
  1310.   DirectVideo:=False;
  1311.   Writeln('This is written via BIOS interrupt calls (int 10h)');
  1312.   Assign(Output,'');
  1313.   Append(Output);
  1314.   Writeln('This is written via DOS calls (int 21h)');
  1315. END.
  1316. --------------------------------------------------------------------
  1317.  
  1318. From ts@uwasa.fi Sun Dec 26 00:00:27 1993
  1319. Subject: Writing an expression parser
  1320.  
  1321. 27. *****
  1322.  Q: How to evaluate a function given as a string to the program?
  1323.  
  1324.  A: To do this you have to have a routine for parsing and evaluating
  1325. your expression. This is a complicated task requiring a clever use
  1326. of recursion. You can find such code in Stephen O'Brien (1988),
  1327. Turbo Pascal, The Complete Reference. Borland-Osborne/McGraw-Hill,
  1328. Chapter 10. Another, simpler piece of code can be found in Michael
  1329. Yester (1989), Using Turbo Pascal, Que, Chapter 5.
  1330.    I've also written such a function evaluation program myself, and
  1331. much of it is based on the ideas in O'Brien with my own corrections
  1332. and enhancements. The resulting program is available as fn.exe
  1333. function evaluator in the /pc/ts/tsfunc13.zip package (or whatever
  1334. version number is the latest). Note however, that the source code is
  1335. not included, nor available.
  1336.    Tips from Justin Lee (ossm1jl@rex.uokhsc.edu):
  1337. 9158 Apr 25 1992 garbo.uwasa.fi:/pc/turbopas/parse11.zip
  1338. parse11.zip Recursive expression Turbo Pascal parser from Ron Loewy
  1339. An excellent parser is included with all the Turbo Pascal versions
  1340. since TP4.0 as part of the MCALC or TCALC spreadsheet example
  1341. program. See mcparse.pas or tcparse.pas.
  1342. --------------------------------------------------------------------
  1343.  
  1344. From ts@uwasa.fi Sun Dec 26 00:00:28 1993
  1345. Subject: Detecting redirection
  1346.  
  1347. 28. *****
  1348.  Q: How does one detect whether input (or output) is redirected?
  1349.  
  1350.  A: As we know input to a program can come from a file, from the
  1351. console, or from a pipe or redirection. Examples of the latter are
  1352.      type text.dat | program
  1353.      program < text.dat
  1354. A Turbo Pascal program can be made to detect the redirections using
  1355. Interrupt 21Hex, function 44Hex, subfunction 00Hex. See PC Magazine
  1356. April 16, 1991, p. 374 for the code, and Duncan (1988), Advanced
  1357. MS-DOS Programming, pp. 412-413 for more information. Alternatively,
  1358. you can utilize the preprogrammed routines
  1359.   PIPEDIFN Is the standard input from redirection
  1360.   PIPEDNFN Is the standard output redirected to nul
  1361.   PIPEDOFN Is the standard output redirected
  1362. from my garbo.uwasa.fi:/pc/ts/ tspa33??.zip units.
  1363. --------------------------------------------------------------------
  1364.  
  1365. From ts@uwasa.fi Sun Dec 26 00:00:29 1993
  1366. Subject: Setting the 43/50 line text mode
  1367.  
  1368. 29. *****
  1369.  Q: How does one set the 43/50 line text mode?
  1370.  
  1371.  A: Quite simple. Just apply TextMode (C80 + font8x8).  Requires a
  1372. "uses Crt;". First, however, you should test that you have a at
  1373. least an EGA video adapter. (See DetectGraph in your TP manual).
  1374. Also see TSUTLE.NWS in garbo.uwasa.fi:/pc/ts/tsutle22.zip (or
  1375. whichever version number is the current) for the non-standard wide
  1376. text modes like 132x43.
  1377. --------------------------------------------------------------------
  1378.  
  1379. From ts@uwasa.fi Sun Dec 26 00:00:30 1993
  1380. Subject: Assigning environment variable values
  1381.  
  1382. 30. *****
  1383.  Q: How can I assign a value to an environment variable in TP?
  1384.  
  1385.  A: For assigning a value to (a parent process's) environment value
  1386. you have to access and manipulate the Program Segment Prefix and
  1387. Memory Control Blocks. This is a rather complicated undertaking. A
  1388. source code with an accompanying article by Trudy Neuhaus can be
  1389. found in PC Magazine Volume 11 Number 1 pages 425-427.
  1390.    The budding TP programmers should note that the elementary trick
  1391. of Exec (GetEnv('comspec'), '/c set key=whatever') will achieve only
  1392. a transient result for the duration of the exec shell. When you exit
  1393. the shell after this endeavor, the environment will be as it was.
  1394.    Here is about the why. When the above command is executed, MsDos
  1395. makes a copy of the environment, and uses the copy. When the above
  1396. shelling terminates, the copy of the environment is deleted, and the
  1397. original is restored. Hence the above trick cannot be used to change
  1398. the parent environment.
  1399.    If you don't want to try to go through this rather complicated
  1400. task yourself, the routines
  1401.  "SETENV   Set a parent environment variable (variable=value)"
  1402.  "SETENVSH Set an environment variable for the duration of shelling"
  1403. can be found in my TP TPU collection garbo.uwasa.fi:/pc/ts/
  1404. tspa33*.zip (* = 40,50,55,60,70). No source code is included, nor
  1405. available, though.
  1406.    One further detail. Users sometimes ask how one can change the
  1407. prompt or the path from within a Turbo Pascal program. This is in no
  1408. way different from changing the value of any other environment
  1409. variable. Both PATH and PROMPT are environment variables that can be
  1410. set with the MsDos SET command in the fashion described in the
  1411. above. This is not changed in any way by the fact that you can apply
  1412. PROMPT and PATH also in an alternative format not requiring the SET
  1413. command.
  1414. --------------------------------------------------------------------
  1415.